<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      class="login-form"
      auto-complete="on"
      :model="loginForm"
      :rules="loginRules"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">{{ title }}</h3>
      </div>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          name="username"
          type="text"
          placeholder="用户名"
        >
          <span slot="prefix" class="svg-container"><svg-icon icon-class="user" /></span>
        </el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          name="password"
          :type="passwordType"
          auto-complete="on"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <span slot="prefix" class="svg-container"><svg-icon icon-class="password" /></span>
        </el-input>
        <span class="show-pwd" @click="showPwd">
          <svg-icon icon-class="eye" />
        </span>
      </el-form-item>

      <el-button
        type="primary"
        style="width:100%;margin-bottom:30px;"
        :loading="loading"
        @click.native.prevent="handleLogin"
      >
        登录
      </el-button>

      <el-row>
        <el-col :span="12">
          <el-button type="text" @click="register">现在注册</el-button>
        </el-col>
        <el-col :span="12" style="text-align:right;">
          <el-button type="text">忘记密码</el-button>
        </el-col>
      </el-row>
    </el-form>

  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 4) {
        callback(new Error('密码至少4位以上'))
      } else {
        callback()
      }
    }
    return {
      title: '系统登录',
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '用户名不能为空' }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'password',
      loading: false,
      showDialog: false
    }
  },

  created() {
  },
  destroyed() {
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },

    handleLogin() {
      // 取到表单组件
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true

          // 向store发送一个action消息（vuex）
          this.$store.dispatch('user/login', this.loginForm) // vuex
            .then(() => {
              // 存储登录时间
              // this.$store.dispatch('login/setLoginTime', new Date())

              this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
            .finally(() => {

            })
        } else {
          return false
        }
      })
    },

    register() {
      this.$router.push({ path: './register' })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;
$cursor: #fff;

.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  // background-color: $bg;

  .login-form {
    border-radius: 8px;
    border: 1px solid #eaeaea;
    box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);
    position: absolute;
    left: 0;
    right: 0;
    width: 520px;
    padding: 35px 35px 15px 35px;
    margin: 80px auto;
  }

  .svg-container {
    color: $dark_gray;
    vertical-align: middle;
    width: 25px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      color: $dark_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 0px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
